package com.gz.moreaseManager.modules.service.impl;

import com.gz.moreaseManager.modules.entity.Product;
import com.gz.moreaseManager.modules.entity.ProductPhoto;
import com.gz.moreaseManager.modules.entity.ProductTag;
import com.gz.moreaseManager.modules.mapper.ProductMapper;
import com.gz.moreaseManager.modules.mapper.ProductTagMapper;
import com.gz.moreaseManager.modules.service.IProductImgService;
import com.gz.moreaseManager.modules.service.IProductPhotoService;
import com.gz.moreaseManager.modules.service.IProductService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.modules.service.IProductSizeService;
import com.gz.moreaseManager.modules.transfer.ProductSimpVo;
import com.gz.moreaseManager.modules.transfer.ProductTagListVo;
import com.gz.moreaseManager.modules.transfer.ProductVo;
import com.gz.moreaseManager.sys.entity.Dict;
import com.gz.moreaseManager.sys.mapper.DictMapper;
import com.gz.moreaseManager.utils.UtilTool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品信息 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-27
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private IProductImgService imgService;
    @Resource
    private IProductPhotoService photoService;
    @Resource
    private IProductSizeService sizeService;

    @Resource
    private ProductTagMapper productTagMapper;
    @Resource
    private DictMapper dictMapper;

    @Transactional
    @Override
    public Boolean addProduct(ProductVo productVo) {

        Product product = new Product();
        BeanUtils.copyProperties(productVo,product);
        if( UtilTool.isNotBlank(productVo.getTagIds())){
            product.setTags( StringUtils.strip(productVo.getTagIds().toString(),"[]").replace(" ", "") );
        }
        //1.新增产品基本信息
        Integer result = productMapper.insert( product );

        if( result == 1 )
            saveDetailInfo(productVo,product.getProductId());
        return true;
    }

    @Transactional
    @Override
    public Boolean editProduct(ProductVo productVo) {
        Product product = new Product();
        BeanUtils.copyProperties(productVo,product);
        if( UtilTool.isNotBlank(productVo.getTagIds())){
            product.setTags( StringUtils.strip(productVo.getTagIds().toString(),"[]").replace(" ", "") );
        }
        //1.新增产品基本信息
        Integer result = productMapper.updateById( product );

        if( result == 1 )
            saveDetailInfo(productVo,product.getProductId());
        return true;
    }

    @Transactional
    @Override
    public Boolean deleteProduct(Integer productId) {
        if( UtilTool.isNotBlank( productId )){
            //1.删除尺码
            sizeService.deleteProductSizeByProductId( productId );
            //2.删除图片
            imgService.deleteImgByProductId( productId );
            //3.删除相册
            photoService.deleteByProductId( productId );
            //4.删除产品信息
            productMapper.deleteById( productId );
        }
        return true;
    }

    @Override
    public ProductVo findProductById(Integer productId) {
        ProductVo productVo = new ProductVo();
        if( UtilTool.isNotBlank( productId )) {
            Product product = productMapper.selectById(productId);
            BeanUtils.copyProperties(product,productVo);
            //组装标签list
            if( UtilTool.isNotBlank( product.getTags() )){
                String [] tags = product.getTags().split(",");
                List<Integer> tagList = new ArrayList<Integer>();
                for(String tag : tags){
                    tagList.add(Integer.valueOf(tag.trim()));
                }
                productVo.setTagIds( tagList );
            }

            //获取尺码信息
            productVo.setSizeList( sizeService.findSizeListByProductId( productId ));
            //获取图片信息
            productVo.setImgList( imgService.findImgListByProductId( productId ) );
            //获取相册信息
            List<ProductPhoto> photoList = photoService.findPhotoListByProductId( productId );
            for( ProductPhoto photo : photoList){
                if( UtilTool.isNotBlank( photo.getProductImg()) && photo.getProductImg().equals( product.getCovenPhoto()))
                    productVo.setCovenId( photo.getId());
            }
            productVo.setPhotoList( photoList );
        }

        return productVo;
    }

    /**
     * 保存其他详细信息
     * @param productVo
     */
    public void saveDetailInfo(ProductVo productVo,Integer productId){
        //1.删除修改的时候需要删除了的尺码
        if( UtilTool.isNotBlank( productVo.getDelSizeList() ))
            sizeService.deleteBatchIds( productVo.getDelSizeList());

        //2.如果规格尺码信息存在  新增尺码信息
        if(UtilTool.isNotBlank( productVo.getSizeList() ) && productVo.getSizeList().size()>0  )
            sizeService.updateProductSize( productVo.getSizeList() ,productId);
        //4.删除图片信息
        if( UtilTool.isNotBlank( productVo.getDelImgList() ))
            imgService.deleteBatchIds( productVo.getDelImgList() );
        //5.规格图片信息存在 新增图片信息
        if( UtilTool.isNotBlank( productVo.getImgList()))
           imgService.updateImgInfo(productVo.getImgList(),productId);
        //7.删除产品相册信息
        if( UtilTool.isNotBlank( productVo.getDelPhotoList() ))
            photoService.deleteBatchIds( productVo.getDelPhotoList());
        //7.新增产品相册信息
        if( UtilTool.isNotBlank( productVo.getPhotoFiles())){
            photoService.updateProductPhoto( productVo.getPhotoFiles(),productId);
        }

        //8.封面id不为空  则取当前相册的路径
        if( UtilTool.isNotBlank( productVo.getCovenId() ))
            photoService.setCovenPhoto( productVo.getCovenId(),productId);

    }

    /**
     * 获取指定标签产品列表
     * @return
     */
    public ProductTagListVo findTagProductList(Integer tagId ){
        if( UtilTool.isNotBlank( tagId )){
            ProductTagListVo tagListVo = new ProductTagListVo();
            //标签信息(获取所有 获取上一条下一条)
            /*ProductTag productTag = productTagMapper.selectById( tagId );
            if( UtilTool.isNotBlank( productTag )){
                tagListVo.setTagName( productTag.getName() );
                tagListVo.setTagType( productTag.getType() );
            }*/
            List<ProductTag> productTags = productTagMapper.selectList(null);
            Map<Integer ,ProductTag> tagMap = new HashMap();
            if( UtilTool.isNotBlank( productTags )){
                //坐标index  current 当前标签 prev 上一条  next 下一条
                int index = 1,current =1,prev=1,next =productTags.size();
                for(ProductTag tag : productTags ){
                    if( tag.getId() == tagId ){
                        current = index;//当前选择的标签下标
                        tagListVo.setTagType( tag.getType());
                        tagListVo.setTagName( tag.getName());
                        tagListVo.setTagUrl( tag.getTagUrl() );
                    }
                    tagMap.put(index,tag);
                    index++;
                }

                //获取上一条和下一条
                if( current -1 >0)
                    prev = current -1;
                if( current + 1 < productTags.size() )
                    next = current +1;

                //存储上一条标签信息
                if( UtilTool.isNotBlank( tagMap.containsKey(prev))){
                    ProductTag prevTag = tagMap.get(prev);
                    tagListVo.setPrevId( prevTag.getId());
                    tagListVo.setPrevName( prevTag.getName());
                }

                //存储下一条标签信息
                if( UtilTool.isNotBlank( tagMap.containsKey(next))){
                    ProductTag nextTag = tagMap.get(next);
                    tagListVo.setNextId( nextTag.getId());
                    tagListVo.setNextName( nextTag.getName());
                }
            }
            //产品列表信息
            List<Product> productList = productMapper.findProductListByTagId(tagId);
            if( UtilTool.isNotBlank( productList )){
                List<ProductSimpVo> list = new ArrayList<>();
                for( Product product : productList ){
                    ProductSimpVo simpVo = new ProductSimpVo();
                    //复制属性值
                    BeanUtils.copyProperties(product,simpVo );
                    list.add( simpVo );
                }
                tagListVo.setProductList( list );
            }
            return tagListVo;

        }
        return null;
    }

    /**
     * 获取某个类型产品列表
     * @return
     */
    public ProductTagListVo findProductListByTags(Integer type ){
        if( UtilTool.isNotBlank( type )){
            ProductTagListVo tagListVo = new ProductTagListVo();
            //获取系列名称
            Dict dict = dictMapper.selectById(type);
            if( UtilTool.isNotBlank( dict )){
                tagListVo.setTagName( dict.getName() );
                tagListVo.setTagType( type );
            }
            //产品列表信息
            List<ProductSimpVo> list = productMapper.findProductListByTags(type);
            if( UtilTool.isNotBlank( list )){
                tagListVo.setProductList( list );
            }
            return tagListVo;
        }
        return null;
    }

    /**
     * 获取推荐类型产品列表
     * @return
     */
    public List<ProductSimpVo> findRecommendProductList(){

        List<ProductSimpVo> list = productMapper.findProductListByRecommend();
        if( UtilTool.isNotBlank( list )){
            return list ;
        }
        return null;
    }

    @Override
    public List<ProductSimpVo> findSearchProductList(String name) {
        if( UtilTool.isBlank( name )) name= null;
        List<ProductSimpVo> list = productMapper.findSearchProductListByName(name);
        if( UtilTool.isNotBlank( list )){
            return list ;
        }
        return null;
    }

    @Override
    public List<ProductSimpVo> findProductRecommed(List<Integer> tags ,Integer productId){
        List<ProductSimpVo> list = null;
        if( UtilTool.isNotBlank( tags ))
            list = productMapper.findProductRecommed(tags,productId);
        return list;
    }
}
